<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form" style="margin-top: 15px">
    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 100px">笔记标题:</label>
        <div class="layui-input-inline">
            <input type="text" name="title" id="title" autocomplete="off"
                   class="layui-input">
        </div>
        <button class="layui-btn" id="searchBtn" lay-submit lay-filter="searchBtn">查询</button>
    </div>
</form>
<table class="layui-table" id="grid" lay-filter="grid">
</table>
<script th:replace="/common/inc::incJs"></script>
<script>
    $(function () {
        var tableIns = createTable({
            url: 'findNotesList',
            cols: [[
                {field: 'nickName', title: '用户', width: 150}
                , {field: 'account', title: '账号', width: 150},
                {field: 'title', title: '标题', width: 200},
                {field: 'likeNum', title: '点赞数', width: 100},
                {field: 'isAuth', title: '审核状态', width: 100, templet: '#authTpl'},
                {field: 'notesType', title: '笔记类型', width: 150, templet: '#notesTypeTpl'}, {
                    field: 'createTime',
                    title: '创建时间',
                    width: 150
                }, {
                    fixed: 'right',
                    title: '操作',
                    width: 280,
                    align: 'center',
                    toolbar: '#opbar'
                }
            ]]
        });

        layui.table.on('tool(grid)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'del') { //删除
                delRecord("/backend/security/customerNotes/del", obj)
            } else if (layEvent === 'change') { //
                layer.confirm('确认变更笔记状态?', function (index) {
                    changeStatus(data.id, data.status);
                });
            } else if (layEvent === 'authIn') { //
                layer.confirm('确认审核通过?', function (index) {
                    changeAuth(data.id, data.isAuth);
                });
            } else if (layEvent === 'authOut') { //
                layer.confirm('确认审核不通过?', function (index) {
                    changeAuth(data.id, data.isAuth);
                });
            } else if (layEvent === 'edit') { //编辑
                openDialog({
                    url: "/backend/security/customerNotes/detail?id=" + data.id,
                    area: ["70%", "85%"],
                    btn: []
                });
            }
        });


        //变更笔记状态
        function changeAuth(notesId, isAuth) {
            if (isAuth == 0) {
                $.post('/backend/security/customerNotes/update', {id: notesId, isAuth: 1}, function (res) {
                        if (res.code < 0) {
                            layer.msg(res.message);
                        }
                        else {
                            layer.msg("审核成功");
                            tableIns.reload();
                        }
                    }
                )
            } else if (isAuth = 1) {
                $.post('/backend/security/customerNotes/update', {id: notesId, isAuth: 0}, function (res) {
                        if (res.code < 0) {
                            layer.msg(res.message);
                        }
                        else {
                            layer.msg("下架成功");
                            tableIns.reload();
                        }
                    }
                )
            }
        }

        function changeStatus(notesId, status) {
            if (status == 0) {
                $.post('/backend/security/customerNotes/update', {id: notesId, status: 1}, function (res) {
                        if (res.code < 0) {
                            layer.msg(res.message);
                        }
                        else {
                            layer.msg("审核成功");
                            tableIns.reload();
                        }
                    }
                )
            } else if (status = 1) {
                $.post('/backend/security/customerNotes/update', {id: notesId, status: 0}, function (res) {
                        if (res.code < 0) {
                            layer.msg(res.message);
                        }
                        else {
                            layer.msg("审核不成功");
                            tableIns.reload();
                        }
                    }
                )
            }
        }

        layui.form.on('submit(searchBtn)', function (data) {
            tableIns.reload({
                where: data.field
            });
            return false;
        });
    })
</script>
<script type="text/html" id="notesTypeTpl">
    {{#  if(d.notesType ===0){ }}
    <span>草稿</span>
    {{#  } else if(d.notesType ===1){ }}
    <span style="color: green;">正式笔记</span>
    {{#  } }}
</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.status ===0){ }}
    <span style="color: #FF0000;">下架</span>
    {{#  } else { }}
    <span style="color: green;">上架</span>
    {{#  } }}
</script>
<script type="text/html" id="authTpl">
    {{#  if(d.isAuth ===0){ }}
    <span style="color: #FF0000;">待审核</span>
    {{#  } else if(d.isAuth ===1) { }}
    <span style="color: green;">审核通过</span>
    {{#  } }}
</script>
<script type="text/html" id="opbar">
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="edit">详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="change">变更状态</a>
    {{# if(d.isAuth==0){ }}
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="authIn">审核通过</a>
    {{#  } else if(d.isAuth ===1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="authOut">审核不通过</a>
    {{# } }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>
